package com.sk.bilibili.view.fragment.main.home.home;

import android.util.Log;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.sk.bilibili.adapter.vlayout.main.home.HeaderAdapter;
import com.sk.bilibili.adapter.vlayout.main.home.TypeAdapter;
import com.sk.bilibili.adapter.vlayout.main.home.live.BannerAdapter;
import com.sk.bilibili.adapter.vlayout.main.LiveAdapter;
import com.sk.bilibili.bean.Live;
import com.sk.bilibili.bean.LiveBanner;
import com.sk.bilibili.bean.LiveType;
import com.sk.bilibili.callBack.BannerClickCallBack;
import com.sk.bilibili.callBack.LiveClickCallBack;
import com.sk.bilibili.callBack.TypeClickCallBack;
import com.sk.bilibili.dto.Extra;
import com.sk.bilibili.model.fragment.main.home.home.LiveModel;
import com.sk.bilibili.view.activity.LiveActivity;
import com.sk.bilibili.view.fragment.BaseRefreshFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class LiveFragment extends BaseRefreshFragment<LiveModel> implements BannerClickCallBack, TypeClickCallBack, LiveClickCallBack {

    private BannerAdapter bannerAdapter;
    private LiveAdapter recommend;
    private List<LiveType> types;
    private List<LiveAdapter> liveAdapters;
    private TypeAdapter typeAdapter;

    @Override
    protected void afterSetView() {
        super.afterSetView();
        types=null;
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        if(types==null){
            model.loadType();
        }else {
            model.loadTypeLive(types);
        }
    }

    @Override
    protected void initAdapter(DelegateAdapter adapter) {
        bannerAdapter=new BannerAdapter(getContext(),this);
        adapter.addAdapter(bannerAdapter);
        typeAdapter = new TypeAdapter(getContext(), this);
        adapter.addAdapter(typeAdapter);
        adapter.addAdapter(new HeaderAdapter(getContext(),"推荐直播"));
        recommend = new LiveAdapter(getContext(), this);
        adapter.addAdapter(recommend);
    }

    @Override
    public void onBannerClick(String url) {
        toast(url);
    }

    public void loadLiveBannerSuccess(List<LiveBanner> liveBanners) {
        bannerAdapter.setList(liveBanners);
        cancelRefresh();
    }

    @Override
    public void onTypeClick(int pos) {
        toast("TYPE:"+pos);
    }

    @Override
    public void onLiveClick(Live live) {
        LiveActivity.start(getContext(),live);
    }

    public void loadRecommendSuccess(List<Live> lives) {
        recommend.setData(lives);
    }

    public void loadLiveTypeSuccess(List<LiveType> liveTypes) {
        this.types=liveTypes;
        liveAdapters=new ArrayList<>();
        for (LiveType liveType:types){
            adapter.addAdapter(new HeaderAdapter(getContext(),liveType.getTitle()));
            LiveAdapter liveAdapter = new LiveAdapter(getContext(), this);
            adapter.addAdapter(liveAdapter);
            liveAdapters.add(liveAdapter);
        }
        adapter.notifyDataSetChanged();
        model.loadTypeLive(liveTypes);
    }

    public void loadTypeLiveSuccess(int pos, List<Live> lives) {
        liveAdapters.get(pos).setData(lives);
    }

    public void loadTypeSuccess(List<Extra> list) {
        typeAdapter.setData(list);
    }

}